package com.guw.TestThread;

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread,实现多线程同步下载图片
public class Test02 extends Thread{
    private String url;
    private String name;

    public Test02(String url,String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownLoad webDownLoad = new WebDownLoad();
        webDownLoad.downloader(url,name);
        System.out.println("下载的文件名为"+name);
    }
    public static void main(String[] args) {
        Test02 t1 = new Test02("http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1115%2F0ZR1095111%2F210ZP95111-10-1200.jpg","1.jpg" );
        Test02 t2 = new Test02("http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1115%2F0ZR1095111%2F210ZP95111-10-1200.jpg","2.jpg" );
        t1.start();
        t2.start();
    }
}

//下载器
class WebDownLoad{
    //创建下载方法
    public void downloader(String url,String name){
        try {
            //将网页上的url下载为文件
            //导入外部的jar包commons-io-2.11.0.jar,使用其中的方法
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }
    }
}